package com.isoft.systemprovidor.service;

import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.systemprovidor.bean.ResponseData;
import com.isoft.systemprovidor.bean.ResultCode;
import com.isoft.systemprovidor.dao.PhoneBookDao;
import com.isoft.systemprovidor.entity.PhoneBook;
import com.isoft.systemprovidor.util.IdUtil;
import com.isoft.systemprovidor.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class PhoneBookService {
    @Autowired
    PhoneBookDao phoneBookDao;

    /*分页获取分页电话簿数据*/
    public ResponseData getallPhoneBookPage(Integer currentPage, Integer size , String department, String username , String job){
        ResponseData responseData = new ResponseData();
        // 检查部门是否为空
        if(department.isEmpty() || department == null){
            responseData.setErrorCode(ResultCode.PARAM_IS_BLANK.getCode());
            responseData.setMsg("部门参数为空");
            return responseData;
        }
        Page<PhoneBook> phoneBookPage = new Page<>();
        // 初始化分页参数
        if (currentPage == null){
            currentPage = 1;
        }
        if (size == null){
            size =10;
        }
        // 构建查询条件
        phoneBookPage.setCurrent(currentPage).setSize(size);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("username" , username);
        wrapper.like("job" , job);
        if(!department.equals("全部")){
            wrapper.eq("department" ,department);
        }
        wrapper.orderByAsc("id");

        //构造返回Controller 的结果
        responseData.setErrorCode(ResultCode.SUCCESS.getCode());
        responseData.setMsg(ResultCode.SUCCESS.getMessage());
        responseData.setData(phoneBookDao.selectPage(phoneBookPage,wrapper));
        return responseData;
    }


    /*修改电话簿数据*/
    public boolean changePhoneBook(PhoneBook phoneBook){
        if((null == phoneBook)||(!IdUtil.idCheck(phoneBook.getId()))){
            return false;
        }
        return phoneBookDao.updateById(phoneBook)>0;
    }
    //验证手机号是否重复
    public boolean phoneIsExist(String phone){
        //判断电话是否在数据库已存在，若存在返回前端消息为电话已重复
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("phone", phone);
        PhoneBook existingPhoneBook = phoneBookDao.selectOne(wrapper);
        // 如果电话簿记录已存在且不是当前正在更新的记录，返回 false
        if (existingPhoneBook != null) {
            return true;
        }
        return false;
    }


    /*批量导入电话簿信息*/
    public boolean importPhoneBook(String fileUrl){
        if (StringUtil.isEmpty(fileUrl)){
            return false;
        }
        /*将文件路径传给easyExcel进行解释数据*/
        log.info("simpleRead:"+fileUrl);
        // 写法1：JDK8+ ,不用额外写一个DemoDataListener
        // since: 3.0.0-beta1
        String fileName = fileUrl;
        // 这里默认每次会读取100条数据 然后返回过来 直接调用使用数据就行
        // 具体需要返回多少行可以在`PageReadListener`的构造函数设置
        com.alibaba.excel.EasyExcel.read(fileName, PhoneBook.class, new PageReadListener<PhoneBook>(dataList -> {
            for (PhoneBook phoneBook : dataList) {
                log.info("读取到一条数据{}", JSON.toJSONString(phoneBook));
                //持久化操作
                phoneBookDao.importPhoneBook(phoneBook);
            }
        })).sheet().doRead();
        return true;
    }




}
